﻿<UserControl x:Class="Squiggle.UI.Controls.ContactListControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:Squiggle.UI.Controls"
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    xmlns:coll="clr-namespace:System.Collections;assembly=mscorlib"
    xmlns:converters="clr-namespace:Squiggle.UI.Converters"
    xmlns:helpers="clr-namespace:Squiggle.UI.Helpers"    
    xmlns:local="clr-namespace:Squiggle.UI.Controls"
    xmlns:chat="clr-namespace:Squiggle.Client;assembly=Squiggle.Client"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"             
    Width="200" Height="400" Loaded="UserControl_Loaded" >
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="..\Resources\Expanders.xaml" />
                <ResourceDictionary>
                    <CollectionViewSource Source="{Binding Buddies}" x:Key="buddiesCollection" Filter="CollectionViewSource_Filter" />
                    <converters:StatusConverter x:Key="statusConverter" />
                    <Style x:Key="ContactInfoToolTipStyle" TargetType="{x:Type ToolTip}">
                        <Setter Property="SnapsToDevicePixels" Value="True"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type ToolTip}">
                                    <Border BorderBrush="#111111" BorderThickness="1" SnapsToDevicePixels="True">
                                        <Border Width="Auto" Height="Auto" Name="windowFrame" BorderBrush="#999999" BorderThickness="1" SnapsToDevicePixels="True" CornerRadius="0" Background="#525252" TextBlock.Foreground="Black" TextBlock.FontFamily="Verdana" TextBlock.FontSize="11" Margin="0" Padding="3">
                                            <ContentPresenter />
                                        </Border>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <ControlTemplate x:Key="groupTemplate">
                        <Expander 
                        Style="{DynamicResource newExpanderStyle}"
                        Tag="{Binding Name}" 
                        Loaded="Group_Loaded"
                        Collapsed="Group_ExpandChanged" 
                        Expanded="Group_ExpandChanged">
                            <Expander.ContextMenu>
                                <ContextMenu>
                                    <MenuItem Header="{StaticResource MainWindow_SendBroadcastMessage}" Tag="{Binding Items}" Click="SendBroadcastMessageMenu_Click" />
                                    <MenuItem Header="{StaticResource MainWindow_StartGroupChat}" Tag="{Binding Items}" Click="StartGroupChatMenu_Click" />
                                </ContextMenu>
                            </Expander.ContextMenu>
                            <Expander.Header>
                                <TextBlock FontSize="11" FontWeight="Bold">
                        <controls:TextBlockBugFix.Text2>
                            <MultiBinding StringFormat="{}{0} ({1}/{2})">
                                <Binding Path="Name" Converter="{x:Static converters:TitleCaseConverter.Instance}" />
                                <Binding Path="Items" Converter="{x:Static converters:ContactListGroupConverter.Instance}" />
                                <Binding Path="ItemCount" />
                            </MultiBinding>
                        </controls:TextBlockBugFix.Text2>
                                </TextBlock>
                            </Expander.Header>
                            <ItemsPresenter />
                        </Expander>
                    </ControlTemplate>
                    <LinearGradientBrush x:Key="ContactInfoBackground" EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFFCFCFD" Offset="0.04"/>
                        <GradientStop Color="#FFE5E5F1" Offset="0.99"/>
                    </LinearGradientBrush>
                    <LinearGradientBrush x:Key="HeaderBackground" EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFFCFCFC" Offset="0.087"/>
                        <GradientStop Color="#FFECECEC" Offset="0.478"/>
                    </LinearGradientBrush>
                    <LinearGradientBrush x:Key="ToolbarBackground" EndPoint="0.5,1" StartPoint="0.5,0" MappingMode="RelativeToBoundingBox">
                        <GradientStop Color="#FFEEF6FA" Offset="0"/>
                        <GradientStop Color="#FFE4F1F7" Offset="0.9"/>
                        <GradientStop Color="#FFE4F1F6" Offset="1"/>
                    </LinearGradientBrush>
                    <ObjectDataProvider MethodName="GetChangableStatuses" 
            ObjectType="{x:Type helpers:SquiggleUtility}" 
            x:Key="AvailableUserStatus">
                    </ObjectDataProvider>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <UserControl.CommandBindings>
        <CommandBinding Command="{x:Static local:ContactListItem.ChatCommand}" Executed="ChatCommand_Executed" />
        <CommandBinding Command="{x:Static local:ContactListItem.EmailCommand}" Executed="EmailCommand_Executed" />
        <CommandBinding Command="{x:Static local:ContactListItem.SendFileCommand}" Executed="SendFileCommand_Executed" />
    </UserControl.CommandBindings>
    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border Background="{StaticResource HeaderBackground}">
            <Grid DataContext="{Binding Path=LoggedInUser}" >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="10"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="0.20*"/>
                </Grid.RowDefinitions>
                <Image  Margin="5,-5,0,0"  MaxWidth="60" MaxHeight="60" Grid.Row="1" Grid.RowSpan="2" Source="{Binding Properties.DisplayImage}" VerticalAlignment="Top">
                    <Image.Style>
                        <Style TargetType="Image">
                            <Setter Property="Margin" Value="0,0,5,0" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Properties.DisplayImage}" Value="{x:Null}">
                                    <Setter Property="Margin" Value="0" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Image.Style>
                </Image>
                <StackPanel Margin="7,-5,0,0" x:Name="signedInView" Grid.Row="1" Grid.Column="1" Orientation="Horizontal" HorizontalAlignment="Left">

                    <TextBlock Margin="0,0,5,0" VerticalAlignment="Center" Text="{Binding DisplayName}" Foreground="Black" FontSize="16" x:Name="txtLoggedInUser" HorizontalAlignment="Left" />
                    <ComboBox  Grid.Column="1" VerticalAlignment="Center" Style="{DynamicResource ComboBoxStyle}" Width="100" SelectedValue="{Binding Path=Status}"
                              Background="Gray" Focusable="False">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={StaticResource AvailableUserStatus}}"/>
                                <Separator IsEnabled="False"/>
                                <ComboBoxItem PreviewMouseLeftButtonDown="ComboBoxItem_PreviewMouseLeftButtonDown"
                                              PreviewKeyDown="ComboBoxItem_PreviewKeyDown" Content="{DynamicResource MainWindow_TrayMenu_SignOut}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                        <ComboBox.ItemsPanel>
                            <ItemsPanelTemplate>
                                <StackPanel Background="{StaticResource HeaderBackground}"/>
                            </ItemsPanelTemplate>
                        </ComboBox.ItemsPanel>
                        <ComboBox.ItemTemplate>
                            <DataTemplate>
                                <ContentPresenter Content="{Binding Converter={StaticResource statusConverter}}" />
                            </DataTemplate>
                        </ComboBox.ItemTemplate>
                    </ComboBox>
                </StackPanel>
                <controls:DisplayMessageBox DataContext="{Binding}" Margin="0,-5,0,0" VerticalAlignment="Bottom" Grid.Row="2" Grid.Column="1" Width="Auto" SelfUser="{Binding}"/>
                <controls:UpdateNotification Grid.Row="3" DataContext="{Binding DataContext,RelativeSource={RelativeSource AncestorType=UserControl}}"  Grid.ColumnSpan="2" />
            </Grid>
        </Border>
        <Grid Grid.Row="1" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
            </Grid.RowDefinitions>
            <DockPanel Height="30" >
                <DockPanel.ContextMenu>
                    <ContextMenu />
                </DockPanel.ContextMenu>
                <DockPanel.Background>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FFF9F9F9" Offset="0.111"/>
                        <GradientStop Color="#FFE4E4E4" Offset="0.593"/>
                    </LinearGradientBrush>
                </DockPanel.Background>
                <Image Source="/Images/info.png" Cursor="Hand" Width="18" Height="18" Margin="5,0,0,0" DockPanel.Dock="Left" MouseLeftButtonDown="About_MouseLeftButtonDown" />
                <controls:FilterTextBox FilterChanged="FilterTextBox_FilterChanged" VerticalAlignment="Top" Height="25" Margin="5,2,5,2" />
            </DockPanel>
            <ScrollViewer Grid.Row="1" Focusable="False" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden">
                <StackPanel>
                    <ItemsControl Focusable="False" Margin="5" DataContext="{Binding}"                                   
                                  ItemsSource="{Binding Source={StaticResource buddiesCollection}}" 
                                  AlternationCount="2">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <controls:ContactListItem AllowDrop="True" Drop="Buddy_Drop" DragOver="Buddy_DragOver" MouseLeftButtonUp="Buddy_Click">
                                    <controls:ContactListItem.ContextMenu>
                                        <ContextMenu>
                                            <MenuItem Header="{StaticResource Buddy_StartChat}" Tag="{Binding}" Click="StartChat_Click" />
                                            <MenuItem Header="{StaticResource Buddy_SendFile}" Tag="{Binding}" Click="SendFile_Click" />
                                            <MenuItem Header="{StaticResource Buddy_SendEmail}" Tag="{Binding}" Click="SendEmail_Click" />
                                        </ContextMenu>
                                    </controls:ContactListItem.ContextMenu>
                                </controls:ContactListItem>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                        <ItemsControl.GroupStyle>
                            <GroupStyle>
                                <GroupStyle.ContainerStyle>
                                    <Style TargetType="{x:Type GroupItem}">
                                        <Setter Property="Template" Value="{StaticResource groupTemplate}" />
                                    </Style>
                                </GroupStyle.ContainerStyle>
                            </GroupStyle>
                        </ItemsControl.GroupStyle>
                    </ItemsControl>
                </StackPanel>
            </ScrollViewer>
        </Grid>
    </Grid>
</UserControl>